var Search = {

    /* Function init
     * @param void
     * @return void
     * @access public
     * */
    init:function () {
        var self = this;
    },

    search:function(title,url,callback){
        var self = this;

        $.modal({
            contentBg: true,
            contentAlign: 'center',
            content:"<label style='display:block; margin-bottom:10px;'>"+title+" </label><input type='text' class='input' id='searchbox'  />",
            resizable: false,
            stripes:true,
            darkStripes:false,
            actions: {},
            buttons: {
                'Cancel' : {
                    classes :	'grey-gradient',
                    click :		function(modal) { modal.closeModal(); }
                },
                'Search' : {
                    classes :	'blue-gradient',
                    click :		function(modal) {
                        self.doSearch($("#searchbox").val(),modal,url,callback);
                    }
                }
            },
            buttonsAlign: 'center'
        });
    },

    doSearch:function (value, modal, url, callback) {
        var self = this;

        var len = value.length;
        if (len < 1) {
            modal.getModalContentBlock().message('Please enter a value', { append:false, classes:['red-gradient'] });
            return false;
        }
        modal.getModalContentBlock().clearMessages();
        modal.getModalContentBlock().message('Searching, please wait...', { append:false, classes:['orange-gradient'] });

        var query = 'query=' + value;
        /* Send the data to the server and handle the server response */
        $.ajax({
            url:url,
            data:query,
            dataType:'json',
            type:'POST',
            success:function (response) {
                var txt = '';
                if (typeof response.mesg == 'object') {
                    for (megTxt in response.mesg) {
                        txt += response.mesg[megTxt] + '<br />';
                    }
                }
                else {
                    txt = response.mesg
                }
                //* When everything went on smoothly on the server redirect the user to the appropriate page.*//*
                if (response.code === 0) {
                    modal.getModalContentBlock().clearMessages();

                    if(typeof callback == 'function'){
                        callback(modal,response);
                    }
                }
                //* When there are Errors *//*
                else
                if (response.code === 1) {
                    modal.getModalContentBlock().clearMessages();
                    modal.getModalContentBlock().message(txt, { append:false, classes:['red-gradient'] });

                }
            },
            error:function (xhr) {
                console.log(xhr.responseText);
                modal.getModalContentBlock().clearMessages();
                modal.getModalContentBlock().message('Error while contacting server, please try again', { append:false, classes:['red-gradient'] });
            }
        });

    }
};

/* when the page is loaded */
$(document).ready(function () {
    Search.init();
});